import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Action, Store } from '@ngrx/store';
import { Actions, Effect } from '@ngrx/effects';
import {
    TaskActionTypes,
    TaskLoadSuccessAction,
    TaskLoadFailAction,
    TaskAddSuccessAction,
    TaskAddFailAction,
    TaskUpdateSuccessAction,
    TaskUpdateFailAction,
    TaskDeleteSuccessAction,
    TaskDeleteFailAction,
    TaskCompleteSuccessAction,
    TaskCompleteFailAction,
    TaskMoveSuccessAction,
    TaskMoveFailAction,
    TaskMoveAllSuccessAction,
    TaskMoveAllFailAction
} from '../actions/task.action';
import { toPayload } from '@ngrx/effects/src/util';
import * as fromRoot from '../reducers';
import { Router } from '@angular/router';
import { TaskService } from '../services/task.service';
@Injectable()
export class TaskEffects {
    @Effect()
    loadTasks$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.LOAD)
        .map(toPayload)
        .switchMap((taskLists) => this.service$.getByLists(taskLists)
            .map(tasks => new TaskLoadSuccessAction(tasks))
            .catch(err => Observable.of(new TaskLoadFailAction(JSON.stringify(err)))));
    @Effect()
    addTasks$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.ADD)
        .map(toPayload)
        .switchMap((task) => this.service$.add(task)
            .map(t => new TaskAddSuccessAction(t))
            .catch(err => Observable.of(new TaskAddFailAction(JSON.stringify(err))))
        );

    @Effect()
    updateTasks$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.UPDATE)
        .map(toPayload)
        .switchMap((task) => this.service$.update(task)
            .map(t => new TaskUpdateSuccessAction(t))
            .catch(err => Observable.of(new TaskUpdateFailAction(JSON.stringify(err)))));
    @Effect()
    delTasks$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.DELETE)
        .map(toPayload)
        .switchMap((task) => this.service$.del(task)
            .map(t => new TaskDeleteSuccessAction(t))
            .catch(err => Observable.of(new TaskDeleteFailAction(JSON.stringify(err)))));

    @Effect()
    completeTasks$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.COMPLETE)
        .map(toPayload)
        .switchMap((task) => this.service$.complete(task)
            .map(t => new TaskCompleteSuccessAction(t))
            .catch(err => Observable.of(new TaskCompleteFailAction(JSON.stringify(err)))));
    @Effect()
    move$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.MOVE)
        .map(toPayload)
        .switchMap(({ taskId, taskListId }) => this.service$.move(taskId, taskListId)
            .map(task => new TaskMoveSuccessAction(task))
            .catch(err => Observable.of(new TaskMoveFailAction(JSON.stringify(err)))));

    @Effect()
    moveAll$: Observable<Action> = this.actions$
        .ofType(TaskActionTypes.MOVE_ALL)
        .map(toPayload)
        .switchMap(({ srclistId, target }) => this.service$.moveAll(srclistId, target)
            .map(tasks => new TaskMoveAllSuccessAction(tasks))
            .catch(err => Observable.of(new TaskMoveAllFailAction(JSON.stringify(err)))));
    constructor(
        private actions$: Actions,
        private service$: TaskService,
        private store$: Store<fromRoot.State>,
        private router: Router
    ) { }
}
